
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;

import javax.sql.rowset.WebRowSet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class ClassUI extends JFrame implements ActionListener{
	private JTextField txtMaLop;
	private JTextField txtTenLop;
	private JTextField txtSoLuong;
	
	JButton btnNew,btnSave,btnReset,btnDelete,btnExit,btnUpdate;
	
	WebRowSet rs=null;
	DataProcess data=null;
	
	JTable table;
	DefaultTableModel model;

	public ClassUI() {
		getContentPane().setLayout(null);
		
		try {
			try {
				data=(DataProcess)Naming.lookup("//khangutc/dkh");
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		}
		
		JLabel lblDanhMucLop = new JLabel("Danh Muc Lop Hoc");
		lblDanhMucLop.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblDanhMucLop.setForeground(Color.RED);
		lblDanhMucLop.setBounds(284, 11, 130, 28);
		getContentPane().add(lblDanhMucLop);
		
		JLabel lblMaLop = new JLabel("Ma Lop");
		lblMaLop.setBounds(10, 62, 52, 21);
		getContentPane().add(lblMaLop);
		
		txtMaLop = new JTextField();
		txtMaLop.setBounds(83, 62, 151, 20);
		getContentPane().add(txtMaLop);
		txtMaLop.setColumns(10);
		
		JLabel lblTenLop = new JLabel("Ten Lop");
		lblTenLop.setBounds(244, 62, 52, 21);
		getContentPane().add(lblTenLop);
		
		txtTenLop = new JTextField();
		txtTenLop.setBounds(306, 62, 151, 20);
		getContentPane().add(txtTenLop);
		txtTenLop.setColumns(10);
		
		JLabel lblSoLuong = new JLabel("So Luong");
		lblSoLuong.setBounds(467, 65, 46, 14);
		getContentPane().add(lblSoLuong);
		
		txtSoLuong = new JTextField();
		txtSoLuong.setBounds(538, 62, 151, 20);
		getContentPane().add(txtSoLuong);
		txtSoLuong.setColumns(10);
		
		/**
		 * Table Settings
		 */
		String[] header={"Ma Lop","Ten Lop","So Luong"};
		model= new DefaultTableModel(header,0);
		
		table= new JTable(model);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				String id=String.valueOf(table.getValueAt(table.getSelectedRow(), 0));
				String name=String.valueOf(table.getValueAt(table.getSelectedRow(), 1));
				String DVHT=String.valueOf(table.getValueAt(table.getSelectedRow(), 2));
				txtMaLop.setText(id);
				txtTenLop.setText(name);
				txtSoLuong.setText(DVHT);
				btnDelete.setEnabled(true);
				btnUpdate.setEnabled(true);
				btnReset.setEnabled(true);
			}
		});
		fillTable();
		
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setBounds(10, 116, 679, 140);
		getContentPane().add(scrollPane);
		
		btnNew = new JButton("New");
		btnNew.setBounds(20, 267, 89, 23);
		getContentPane().add(btnNew);
		
		btnUpdate = new JButton("Update");
		btnUpdate.setBounds(242, 267, 89, 23);
		getContentPane().add(btnUpdate);
		
		btnDelete = new JButton("Delete");
		btnDelete.setBounds(354, 267, 89, 23);
		getContentPane().add(btnDelete);
		
		btnReset = new JButton("Reset");
		btnReset.setBounds(467, 267, 89, 23);
		getContentPane().add(btnReset);
		
		btnExit = new JButton("Exit");
		btnExit.setBounds(582, 267, 89, 23);
		getContentPane().add(btnExit);
		
		btnSave = new JButton("Save");
		btnSave.setBounds(132, 267, 89, 23);
		getContentPane().add(btnSave);
		
		JButton[] btn={btnNew,btnSave,btnReset,btnDelete,btnExit,btnUpdate};
		int i;
		for(i=0;i<btn.length;i++){
			btn[i].addActionListener(this);
		}
	}
	
	public static void main(String[] args){
		new ClassUI().setVisible(true);
	}
	
	protected void fillTable(){
		try {
			rs= data.GetTable("Class");
			while(rs.next()){
				String[] row={rs.getString(1),rs.getString(2),String.valueOf(rs.getInt(3))};
				model.addRow(row);
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	
	}
	
	/**
	 * Update table khi them moi thanh cong
	 */
	protected void updateRow(){
		String condition="ClassID='" + txtMaLop.getText() + "'";
			try {
				rs=data.GetRecord(condition,"Class");
				while(rs.next()){
					String[] row={rs.getString(1),rs.getString(2),String.valueOf(rs.getInt(3))};
					model.addRow(row);			
				}
			} catch (RemoteException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		
	}
	
	/**
	 * Cap nhat lai cho table khi update thanh cong
	 */
	protected void updateValue() {
		String Ten=txtTenLop.getText();
		String SL=txtSoLuong.getText();
		
		table.setValueAt(Ten,table.getSelectedRow(),1);	
		table.setValueAt(SL,table.getSelectedRow(),2);	
	}
	
	/**
	 * Cap nhat lai cho table khi delete thanh cong
	 */
	protected void updateValuedelete() {
		model.removeRow(table.getSelectedRow());		
	}
	
	/**
	 * Xu ly them moi du lieu
	 */
	protected boolean insertData(String id, String ten, int SL) {
		String sql="insert into Class values('" + id + "','" + ten + "'," + SL + ")";
		try {
			data.ExecUpdateSQL(sql);
			return true;
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * Xu ly delete 1 ban ghi
	 * @param id
	 * @return
	 */
	protected boolean deleteData(String id){
		String sql="delete from Class where ClassID='" + id + "'";
		try {
			data.ExecUpdateSQL(sql);
			return true;
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * Xu ly update du lieu
	 */
	protected boolean updateDate(String id, String name, int SL){
		String sql="update Class set Location='" + name + "',Number=" + SL + " where ClassID='" + id + "'";
		try {
			data.ExecUpdateSQL(sql);
			return true;
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * Ham reset values
	 */
	protected void resetValues(){
		txtMaLop.setText("");
		txtTenLop.setText("");
		txtSoLuong.setText("");
		txtMaLop.setEnabled(false);
		btnNew.setEnabled(true);
		btnDelete.setEnabled(false);
		btnSave.setEnabled(false);
		btnReset.setEnabled(false);
		btnUpdate.setEnabled(false);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==btnNew){
			resetValues();
			txtMaLop.setEnabled(true);
			txtMaLop.requestFocus();
			btnSave.setEnabled(true);
			btnReset.setEnabled(true);
		}
		
		if(e.getSource()==btnSave){
			String id=txtMaLop.getText();
			String name=txtTenLop.getText();
			int sl = Integer.parseInt(txtSoLuong.getText());			
			if(insertData(id, name,sl)){
				JOptionPane.showMessageDialog(null,"Save successed!");
				updateRow();
			}else{
				JOptionPane.showMessageDialog(null,"Insert failed!");
			}
		}
		
		if(e.getSource()==btnReset){
			resetValues();
		}
		
		if(e.getSource()==btnDelete){
			String id=txtMaLop.getText();
			if(deleteData(id)){
				JOptionPane.showMessageDialog(null,"Deleted!");
				updateValuedelete();
			}
			else{
				JOptionPane.showMessageDialog(null,"Delete failed!");
			}
			resetValues();
		}
		
		if(e.getSource()==btnUpdate){
			String id=txtMaLop.getText();
			String name=txtTenLop.getText();
			int Khoa= Integer.parseInt(txtSoLuong.getText());
			if(updateDate(id, name,Khoa)){
				JOptionPane.showMessageDialog(null,"Updated!");
				updateValue();
			}
			else{
				JOptionPane.showMessageDialog(null,"Update failed!");
			}
			resetValues();
		}
		
		if(e.getSource()==btnExit){
			System.exit(0);
		}
		
		
	}

}
